<?php

namespace App\HttpController\Base;

use App\Exceptions\ApiException;
use App\Utility\JwtAuth;
use Throwable;

abstract class AuthBase extends Base
{
    protected $user_id = 0;
    protected $system_app_id = 0;

    protected function onRequest(?string $action): ?bool
    {
        $header = $this->request()->getHeader('app_id');
        $app_id = end($header);
        $jwt_token = $this->request()->getHeader('qien_token');
        $jwt_token = end($jwt_token);
        $result = JwtAuth::decodeToken($jwt_token);
        if (!empty($result['data'])) {
            $this->user_id = $result['data']['user_id'];
            $this->system_app_id = $app_id;
        } else {
            throw new ApiException('token不正确');
        }


        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }


}